<?php

class Slide extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('slide_model');
		$this->load->library('upload');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		//$this->load->library('security');
		$this->load->library('auth/tank_auth');
		$this->lang->load('auth/tank_auth');
		$this->load->model('tank_auth/users');
		$this->load->library('pagination');
		$this->db_front = $this->load->database('front', TRUE);
	}
	
	function index(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				
				$offset = $this->uri->segment(3);
				$limit = '10';
				if(!$offset){
					$page = 0;
				}else{
					$page = $offset; 	
				}
				
				$data['hasil'] = $this->slide_model->getAll($page,$limit);
				
				$config_pagination['base_url']    = site_url('slide/index');
				$config_pagination['total_rows']  = count($this->slide_model->getAll('0','0'));
				$config_pagination['per_page']    = $limit;
				$config_pagination['uri_segment'] = 3;
				$config_pagination['num_links']   = 4;
		
				//pagination style
				$config_pagination['full_tag_open'] = '<div class="pagination"><ul>';
				$config_pagination['full_tag_close'] = '</ul></div>';
		
				$config_pagination['first_link'] = 'First';
				$config_pagination['first_tag_open']  = '<li>';
				$config_pagination['first_tag_close'] = '</li>';
		
				$config_pagination['last_link'] = 'Last';
				$config_pagination['last_tag_open']  = '<li>';
				$config_pagination['last_tag_close'] = '</li>';
		
				$config_pagination['next_link'] = '&gt;';
				$config_pagination['next_tag_open']  = '<li>';
				$config_pagination['next_tag_close'] = '</li>';
		
				$config_pagination['prev_link'] = '&lt;';
				$config_pagination['prev_tag_open'] = '<li>';
				$config_pagination['prev_tag_close'] = '</li>';
		
				$config_pagination['cur_tag_open'] = '<li class="disabled"><a href="#">';
				$config_pagination['cur_tag_close'] = '</a></li>';
		
				$config_pagination['num_tag_open'] = '<li>';
				$config_pagination['num_tag_close'] = '</li>';
		
				$this->pagination->initialize($config_pagination); 
		
				$data['pagination'] = $this->pagination->create_links();
						
				
				$data['content'] = 'slide';
				$this->load->view('common/common',$data);
			}else{
				redirect('/common/');
			}
		}
	}
	
	function add(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			$this->form_validation->set_rules('slide_name','Slide Name','trim|required|xss_clean');
			$this->form_validation->set_rules('slide_image','Slide Image','trim|xss_clean');
			$this->form_validation->set_rules('slide_description',' Description','trim|xss_clean');
			$this->form_validation->set_rules('status','Status','trim|required|xss_clean');
			if ($this->tank_auth->get_url()){
				if ($this->form_validation->run() == FALSE){
						//$this->load->view('slide_form');
						$data['content'] = 'slide_form';
						$this->load->view('common/common',$data);
				}else {
						$config['upload_path'] = './data/slide/';
						$config['allowed_types'] = 'jpg|jpeg|png|gif';
						$config['max_size'] = '5000';							
						$this->load->library('upload', $config);
						$this->upload->initialize($config);
						if (!$this->upload->do_upload('slide_image')){
							//echo $this->upload->display_errors(); 
							$this->slide_model->create($_POST);
						}else{
							//$upload_data = array('upload' => $this->upload->data());
							$upload_data = $this->upload->data();
							//print_r($this->upload->data());
							
							// create thumbnail
								if (isset($upload_data['full_path']) && $upload_data['full_path'] != ''){
									$this->load->library('image_lib');
									$config = array('source_image' => $upload_data['full_path'], 
													'new_image' => './data/slide/thumb/', 
													'maintain_ration' => true, 
													'width' => 600,
													'height' => 250);
					
									$this->image_lib->initialize($config);
									$this->image_lib->resize();
									$this->image_lib->clear();
								}
							
							$this->slide_model->create($_POST);
						}
						redirect('slide/index');
				}
			}else{
				redirect('/common/');
			}
		}
				
          
    }
	
	function updatedata($slide_id){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			$this->form_validation->set_rules('slide_name','Slide Name','trim|required|xss_clean');
			$this->form_validation->set_rules('slide_image','Slide Image','trim|xss_clean');
			$this->form_validation->set_rules('slide_description',' Description','trim|xss_clean');
			$this->form_validation->set_rules('status','Status','trim|required|xss_clean');
			if ($this->tank_auth->get_url()){
				if ($this->form_validation->run() == FALSE){
					$data['hasil'] =  $this->slide_model->select($slide_id);
					//$this->load->view('slide_form_update', $data);
					$data['content'] = 'slide_form_update';
					$this->load->view('common/common',$data);
				}else{
						$config['upload_path'] = './data/slide/';
						$config['allowed_types'] = 'jpg|jpeg|png|gif';
						$config['max_size'] = '5000';							
						$this->load->library('upload', $config);
						$this->upload->initialize($config);
						if (!$this->upload->do_upload('slide_image')){
							//echo $this->upload->display_errors();
							$this->slide_model->update($slide_id);
						}else{
								$config = $this->slide_model->get_slide_by_id($slide_id);
								if($config['slide_image'] == NULL){
									$upload_data = $this->upload->data();
									//print_r($this->upload->data());
									
									// create thumbnail
									if (isset($upload_data['full_path']) && $upload_data['full_path'] != ''){
										$this->load->library('image_lib');
										$config = array('source_image' => $upload_data['full_path'], 
														'new_image' => './data/slide/thumb/', 
														'maintain_ration' => true, 
														'width' => 600,
														'height' => 250);
						
										$this->image_lib->initialize($config);
										$this->image_lib->resize();
										$this->image_lib->clear();
									}
									
									$this->slide_model->update($slide_id);
								}else{
									unlink ('./data/slide/'.$config['slide_image']); 
									unlink ('./data/slide/thumb/'.$config['slide_image']);
									$upload_data = $this->upload->data();
									//print_r($this->upload->data());
									
									// create thumbnail
									if (isset($upload_data['full_path']) && $upload_data['full_path'] != ''){
										$this->load->library('image_lib');
										$config = array('source_image' => $upload_data['full_path'], 
														'new_image' => './data/slide/thumb/', 
														'maintain_ration' => true, 
														'width' => 600,
														'height' => 250);
						
										$this->image_lib->initialize($config);
										$this->image_lib->resize();
										$this->image_lib->clear();
									}
									
									$this->slide_model->update($slide_id);
								}
								
							}
						redirect('slide/index');
				}
			}else{
				redirect('/common/');
			}
		}
    }
	
	function deletedata($slide_id){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				$config = $this->slide_model->get_slide_by_id($slide_id);
				if($config['slide_image'] == NULL){
					$this->slide_model->delete($slide_id);
					redirect('slide/index');
				}else{
					unlink ('./data/slide/'.$config['slide_image']); 
					unlink ('./data/slide/thumb/'.$config['slide_image']); 
					$this->slide_model->delete($slide_id);
					redirect('slide/index');
				}
			}else{
				redirect('/common/');
			}
		}
    }
	
}
?>